package com.prs.crm.dao.cdxh.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.MaterialProductDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.material.MaterialTeam;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-3 下午04:33:38
 * 简单说明
 */

public class MaterialProductDaoImpl extends BaseDaoImpl<MaterialProduct>
		implements MaterialProductDao {

	@Override
	public void saveOrMaterialProduct(MaterialProduct materialProduct) {
		this.getHibernateTemplate().saveOrUpdate(materialProduct);
	}

	@Override
	public MaterialProduct getMaterialProduct(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(MaterialProduct.class, id);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public MaterialProduct getMaterialProductOrPro(final String orderplan,
			final String productCode) {
		return (MaterialProduct)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MaterialProduct.class);
				        	criteria.add(Restrictions.eq("planOrOrderId", orderplan));
				        	criteria.add(Restrictions.eq("productCode", productCode));				        	
				        	List<MaterialProduct> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public MaterialProduct getMaterialProductOrProAndFirst(final String orderplan,
			final String productCode) {
		return (MaterialProduct)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MaterialProduct.class);
				        	criteria.add(Restrictions.eq("planOrOrderId", productCode));
				        	criteria.add(Restrictions.eq("productCode", orderplan));	
				        	criteria.add(Restrictions.eq("firstYN", 2));	
				        	List<MaterialProduct> result = criteria.list();
//				        	System.out.println("逻辑层orderplan="+orderplan);
//				        	System.out.println("逻辑层productCode="+productCode);
//				        	System.out.println("逻辑层result="+result.size());
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<MaterialProduct> getMaterialProductOrderpLan(final String orderplan) {
		return (List<MaterialProduct>)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MaterialProduct.class);
				        	criteria.add(Restrictions.eq("planOrOrderId", orderplan));		        	
				        	List<MaterialProduct> result = criteria.list();				        	
				        		return result;				        	
				        }   
				 } );
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public MaterialProduct getMP(final String orderplan,final String productCode,
			final int pLstate) {
		return (MaterialProduct)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MaterialProduct.class);
				        	criteria.add(Restrictions.eq("planOrOrderId", orderplan));
				        	criteria.add(Restrictions.eq("productCode", productCode));		
				        	criteria.add(Restrictions.eq("pLState", pLstate));	
				        	List<MaterialProduct> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	@Override
	public MaterialProduct getMaterialProductByProductCode(final String productCode) {
		return (MaterialProduct)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MaterialProduct.class);
				        	criteria.add(Restrictions.eq("productCode", productCode));	
				        	List<MaterialProduct> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

}
